﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Actya.Web.Infrastructure.ElFinderConnector.Commands;
using Actya.Web.Infrastructure.ElFinderConnector.Responses;

namespace Actya.Web.Infrastructure.ElFinderConnector
{
    public class CommandDispatcher
    {
        protected System.Collections.Specialized.NameValueCollection Parameters { get; set; }

        protected HttpFileCollectionBase Files { get; set; }

        protected Dictionary<string, Func<ICommand>> Commands = new Dictionary<string, Func<ICommand>>();

        public CommandDispatcher(HttpRequestBase request)
        {
            this.Parameters = request.QueryString.Count>0 ? request.QueryString : request.Form;
            this.Files = request.Files;
        }

        public Response DispatchCommand()
        {
            var cmdName = Parameters["cmd"];

            if (string.IsNullOrEmpty(cmdName))
            {
                return new ErrorResponse("Command not set");
            }

			if (Configuration.Configuration.DisabledCommands.Contains(cmdName))
			{
				return new ErrorResponse("Command is not allowed");
			}

            ICommand cmd = null;

            switch (cmdName)
            {                
                case "open":
                    if (!string.IsNullOrEmpty(Parameters["init"]) && (Parameters["init"] == "true" || Parameters["init"] == "1"))
                        cmd = new Init();
                    else
                    {
                        cmd = new Open(Parameters["target"]);                        
                    }
                    break;
                case "mkdir":
                    cmd = new MkDir(Parameters["current"], Parameters["name"]);
                    break;
                case "rm":
                    cmd = new Rm(Parameters["current"], Parameters["targets[]"]);
                    break;
                case "rename":
                    cmd = new Rename(Parameters["current"], Parameters["target"], Parameters["name"]);
                    break;
                case "upload":                    
                    cmd = new Upload(Parameters["current"], Files);
                    break;
                case "ping":
                    cmd = new Ping();
                    break;
                case "tmb":
                    cmd = new Tmb(Parameters["current"]);
                    break;
                case "duplicate":
                    cmd = new Duplicate(Parameters["current"], Parameters["target"]);
                    break;
                case "paste":
                    cmd = new Paste(Parameters["src"], Parameters["dst"], Parameters["targets[]"], Parameters["cut"]);
                    break;
                case "archive":
                    cmd = new Archive(Parameters["current"], Parameters["type"], Parameters["targets[]"]);
                    break;
				case "resize":
            		cmd = new Resize(Parameters["current"], Parameters["target"], Convert.ToInt32(Parameters["width"]),
            		                 Convert.ToInt32(Parameters["height"]), Parameters["mode"],
            		                 Convert.ToInt32(Parameters["x"]), Convert.ToInt32(Parameters["y"]));
            		break;
            }

            if (cmd == null)
            {
                return new ErrorResponse("Unknown command");
            }

            return cmd.Execute();
        }
    }
}
